package sng.modules.view.components.city.logic.unit
{
	import core.interfaces.IDispose;
	
	import org.casalib.util.ArrayUtil;
	
	import sng.modules.model.vo.UserBuildingVO;
	import sng.modules.view.components.city.logic.City;

	//Create By royjin 20110708
	//道路显示管理逻辑类
	public final class RoadManager extends Object implements IDispose
	{
		private var city:City;					//城市引用
		private var roadList:Array;			//城市中道路数组
		
		private var preViewRoad:BaseBuilding;	//预览用道路对象
		
		public function RoadManager(cy:City)
		{
			this.city = cy;
			roadList = new Array();
		}
		public function dispose():void
		{
			roadList = new Array();
			preViewRoad = null;
		}
		//重置所有道路形象
		public function Reset():void						
		{
		
		}
		//添加一个预浏道路
		public function AddPreViewRoad(pr:BaseBuilding):void
		{
			//trace("添加一个御览道路,x:"+preViewRoad.column+",y:"+preViewRoad.row);
			preViewRoad = pr;
			this.AddRoad(preViewRoad);
		}
		//删除一个预浏道路
		public function DelPreViewRoad():void
		{
			if(preViewRoad!=null)
			{
				//trace("删除一个御览道路,x:"+preViewRoad.column+",y:"+preViewRoad.row);
				this.DelRoad(preViewRoad);
				this.UpdateRoadSkin(preViewRoad,0);
				preViewRoad=null;
			}
		}
		//添加一个道路
		public function AddRoad(road:BaseBuilding):void	
		{
			roadList.push(road);
			this.UpdateRoad(road);
		}
		//删除一个道路
		public function DelRoad(road:BaseBuilding):void	
		{
			ArrayUtil.removeItem(this.roadList,road);
			this.UpdateRoad(road);
		}
		//更新路面形象
		private function UpdateRoad(road:BaseBuilding):void
		{
			//获取当前和上下左右四个邻近的路的tile数组
			var nearList:Array = new Array();
			nearList.push(road);
			nearList.push(this.GetRoad(road.column,road.row-2));
			nearList.push(this.GetRoad(road.column+2,road.row));
			nearList.push(this.GetRoad(road.column,road.row+2));
			nearList.push(this.GetRoad(road.column-2,road.row));
			//循环数组调用更新单个路面形象逻辑
			for each(var i:Object in nearList)
			{
				if(i is BaseBuilding)
				{
					UpdataRoadLogic(BaseBuilding(i));
				}
			}
		}
		//获取一个指定行列上的road
		private function GetRoad(col:int,row:int):BaseBuilding
		{
			var res:BaseBuilding;
			for each(var i:BaseBuilding in roadList)
			{
				if(i.column==col&&i.row==row)
				{
					res = i;
					break;
				}
			}
			return res;
		}
		//更新单个路面形象逻辑
		private function UpdataRoadLogic(road:BaseBuilding):void			
		{ 
			//邻近的北,东,南,西的路段对象
			var roadN:BaseBuilding =this.GetRoad(road.column,road.row-2);
			var roadE:BaseBuilding =this.GetRoad(road.column+2,road.row);
			var roadS:BaseBuilding =this.GetRoad(road.column,road.row+2);
			var roadW:BaseBuilding =this.GetRoad(road.column-2,road.row);
			
			//判断计数,北有路是2,东有路是4,南有路是8,西有路是1
			var addNum:int = 0;
			if(roadN!=null){addNum+=2;}
			if(roadE!=null){addNum+=4;}
			if(roadS!=null){addNum+=8;}
			if(roadW!=null){addNum+=1;}
			//皮肤序号
			var sheetIndex:int=0;
			
			switch(addNum)
			{
				case 0:					//没有出口逻辑
					sheetIndex=0;
					break;
				case 2:					//北面一个出口逻辑
					sheetIndex=1;
					break;
				case 4:					//东面一个出口逻辑
					sheetIndex=2;
					break;
				case 8:					//南面一个出口逻辑
					sheetIndex=3;
					break;
				case 1:					//西面一个出口逻辑
					sheetIndex=4;
					break;
				case 6:					//东北二个出口逻辑
					sheetIndex=5;
					break;				
				case 12:				//东南二个出口逻辑
					sheetIndex=6;		
					break;
				case 9:					//西南二个出口逻辑
					sheetIndex=7;
					break;
				case 3:					//西北二个出口逻辑
					sheetIndex=8;
					break;
				case 10:				//北南有二个出口逻辑
					sheetIndex=9;
					break;
				case 5:					//西东有二个出口逻辑
					sheetIndex=10;
					break;
				case 14:				//北东南三个出口逻辑
					sheetIndex=11;
					break;
				case 13:				//东南西三个出口逻辑
					sheetIndex=12;
					break;
				case 11:				//南西北三个出口逻辑
					sheetIndex=13;
					break;
				case 7:					//西北东三个出口逻辑
					sheetIndex=14;
					break;
				case 15:				//四面出口逻辑
					sheetIndex=15;
					break;
				default:
					sheetIndex=0;
					break;
			}
			this.UpdateRoadSkin(road,sheetIndex);
		}
		//更新路面皮肤
		private function UpdateRoadSkin(road:BaseBuilding,sheetIndex:int):void
		{
			road.skin.Reload("assets/road/"+sheetIndex+".swf");	
		}
		
	}
}